package cn.chinasyq.photoquan.fragment;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.view.ZoomImageView;
import cn.master.volley.commons.VolleyHelper;


public class PreviewNetWorkPictureFragment extends Fragment {

	private ZoomImageView zoomImageView;
	private FrameLayout layout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_network_preview,container,false);
		zoomImageView = (ZoomImageView) view.findViewById(R.id.zoomImageView);
		layout = (FrameLayout) view.findViewById(R.id.layout);
		Uri uri = Uri.parse(getArguments().getString("url"));
	if (uri.getScheme().equals("file")||uri.getScheme().equals("content")){
		zoomImageView.setImageURI(uri);
		layout.setVisibility(View.GONE);
	}else {
		VolleyHelper.getImageLoader().get(uri.toString(), new ImageLoader.ImageListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
			}

			@Override
			public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
				if (null != response.getBitmap()) {
					zoomImageView.setImageBitmap(response.getBitmap());
					layout.setVisibility(View.GONE);
				}
			}
		});
	}
		return view;
	}

	@Override
	public void onDestroy() {
		if (zoomImageView != null){

			Bitmap bitmap = zoomImageView.getDrawingCache();
			if (bitmap != null && !bitmap.isRecycled()){
				bitmap.recycle();
				bitmap = null;
			}
		}
		super.onDestroy();
	}
}
